home *** CD-ROM | disk | FTP | other *** search
/ SGI Developer Toolbox 6.1 / SGI Developer Toolbox 6.1 - Disc 4.iso / public / radio / playulaw.c < prev    next >
C/C++ Source or Header  |  1994-08-01  |  4KB  |  194 lines

  1. /***********************************************************
  2. Copyright 1991, 1992, 1993 by Stichting Mathematisch Centrum,
  3. Amsterdam, The Netherlands.
  4.  
  5.                         All Rights Reserved
  6.  
  7. Permission to use, copy, modify, and distribute this software and its 
  8. documentation for any purpose and without fee is hereby granted, 
  9. provided that the above copyright notice appear in all copies and that
  10. both that copyright notice and this permission notice appear in 
  11. supporting documentation, and that the names of Stichting Mathematisch
  12. Centrum or CWI not be used in advertising or publicity pertaining to
  13. distribution of the software without specific, written prior permission.
  14.  
  15. STICHTING MATHEMATISCH CENTRUM DISCLAIMS ALL WARRANTIES WITH REGARD TO
  16. THIS SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND
  17. FITNESS, IN NO EVENT SHALL STICHTING MATHEMATISCH CENTRUM BE LIABLE
  18. FOR ANY SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
  19. WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
  20. ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
  21. OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  22.  
  23. ******************************************************************/
  24.  
  25. /* Play ulaw audio data read from stdin */
  26.  
  27. #define BUFFERSIZE 4000
  28.  
  29. #ifdef sgi
  30. #define USE_AL
  31. #endif
  32. #ifdef sun
  33. #define USE_SUN
  34. #endif
  35.  
  36. #include <stdio.h>
  37. #include <errno.h>
  38. #include <stdlib.h>
  39. #include <fcntl.h>
  40. #include <signal.h>
  41.  
  42. #include <sys/types.h>
  43. #include <sys/socket.h>
  44. #include <sys/time.h>
  45. #include <netinet/in.h>
  46.  
  47. #ifdef USE_AL
  48. #include <audio.h>
  49. #include "libst.h"
  50.  
  51. long savestate[] = {
  52.     AL_OUTPUT_RATE, 0,
  53. };
  54. #endif
  55.  
  56. #ifdef USE_SUN
  57. #include <stropts.h>
  58. #endif
  59.  
  60. /* getopt() interface */
  61. extern int optind;
  62. extern char * optarg;
  63.  
  64. /* Forward */
  65. void cleanup_handler();
  66.  
  67. /* Globals */
  68. char *progname;
  69.  
  70. main(argc, argv)
  71.     int argc;
  72.     char **argv;
  73. {
  74.     char buf[BUFFERSIZE];
  75.     int n;
  76.     int c;
  77.     int ifd, ofd;
  78.     int sts = 0;
  79. #ifdef USE_AL
  80.     short obuf[BUFFERSIZE];
  81.     ALport aport;
  82.     ALconfig config;
  83.     int i;
  84.     long pvbuf[2];
  85. #endif
  86.  
  87.     progname = argv[0];
  88.     while ((c = getopt(argc, argv, "")) != EOF) {
  89.         switch (c) {
  90.         case '?':
  91.             usage();
  92.         }
  93.     }
  94.  
  95.     if (optind >= argc) {
  96.         ifd = fileno(stdin);
  97.     }
  98.     else {
  99.         if (optind+1 < argc)
  100.             usage();
  101.         ifd = open(argv[optind], 0);
  102.         if (ifd < 0) {
  103.             perror(argv[optind]);
  104.             exit(1);
  105.         }
  106.     }
  107.  
  108. #ifdef USE_AL
  109.     /* Fetch the original state */
  110.     ALgetparams(AL_DEFAULT_DEVICE, savestate,
  111.             sizeof(savestate) / sizeof(long));
  112.  
  113.     /* Set signal handlers */
  114.     signal(SIGINT, cleanup_handler);
  115.     signal(SIGTERM, cleanup_handler);
  116.  
  117.     /* Configure and open an SGI audio port */
  118.     config = ALnewconfig();
  119.     ALsetchannels(config, AL_MONO);
  120.     ALsetwidth(config, AL_SAMPLE_16);
  121.     ALsetqueuesize(config, 16000); /* 2 seconds slop */
  122.     aport = ALopenport("radio", "w", config);
  123.     if (aport == NULL) {
  124.         perror("ALopenport");
  125.         exit(1);
  126.     }
  127.  
  128.     /* Set the output sampling rate to 8000 Hz */
  129.     /* Do this after ALopenport so we needn't undo it if that fails */
  130.     pvbuf[0] = AL_OUTPUT_RATE;
  131.     pvbuf[1] = AL_RATE_8000;
  132.     ALsetparams(AL_DEFAULT_DEVICE, pvbuf, 2L);
  133. #else
  134.     /* Write to /dev/audio */
  135.     if ((ofd = open("/dev/audio", O_WRONLY | O_NDELAY)) < 0) {
  136.         perror("/dev/audio");
  137.         exit(1);
  138.     }
  139. #endif
  140.  
  141.     for (;;) {
  142.         n = read(ifd, buf, BUFFERSIZE);
  143.         if (n <= 0) {
  144.             if (n < 0) {
  145.                 perror("read");
  146.                 sts = 1;
  147.             }
  148.             break;
  149.         }
  150. #ifdef USE_AL
  151.         for (i = 0; i < n; i++)
  152.             obuf[i] = st_ulaw_to_linear(buf[i]);
  153.         ALwritesamps(aport, obuf, (long)n);
  154. #else
  155.         if (write(ofd, buf, n) != n) {
  156.             perror("write");
  157.             sts = 1;
  158.             break;
  159.         }
  160. #endif
  161.     }
  162.  
  163. #ifdef USE_AL
  164.     /* Wait until all sound has played */
  165.     while(ALgetfilled(aport) > 0) /* still sound to play */
  166.         sginap(1);    /* sleep for 1/60 of a second */
  167.  
  168.     /* Restore the output sampling rate */
  169.     ALsetparams(AL_DEFAULT_DEVICE, savestate,
  170.             sizeof(savestate) / sizeof(long));
  171. #endif
  172.  
  173.     exit(sts);
  174. }
  175.  
  176. usage()
  177. {
  178.     fprintf(stderr, "usage: %s [file]\n", progname);
  179.     exit(2);
  180. }
  181.  
  182. #ifdef USE_AL
  183.  
  184. void cleanup_handler(sig)
  185.     int sig;
  186. {
  187.     signal(sig, SIG_DFL);
  188.     ALsetparams(AL_DEFAULT_DEVICE, savestate,
  189.             sizeof(savestate) / sizeof(long));
  190.     kill(getpid(), sig);
  191. }
  192.  
  193. #endif /* USE_AL */
  194.